﻿<div style="height: 40px; line-height: 40px; padding: 0 10px;">
    <div class="pull-left" style="height: 40px; line-height: 40px;">
        <h2 id="bigTitle">
            @ViewBag.Title
        </h2>
    </div>
</div>
<hr />
@Html.Partial("MessageTip")

<div class="tishi">
    提示：双击可修改详细信息，右键修改页面SEO信息，勾选复选框可设置是否为热门数据&nbsp;&nbsp;<span id="updateTip"></span>
</div>
<ul id="productTree" class="ztree" style="width: 600px; margin: 0 auto 5px auto;">
</ul>
@section scripts{
 <script type="text/javascript">
     jQuery(document).ready(function () {
         $.getJSON('@Url.Action("ProductTree", "DataProduct")',
                function (data) {
                    changeProperity(data);
                    $.fn.zTree.init($("#productTree"), setting, data);

                    var zTree = $.fn.zTree.getZTreeObj("productTree");
                    zTree.setting.edit.showRemoveBtn = true;
                    zTree.setting.edit.showRenameBtn = false;
                    zTree.setting.edit.removeTitle = '删除';
                    zTree.setting.edit.renameTitle = '编辑SEO';
                    zTree.setting.check.chkboxType = { "Y": "", "N": "" };
                });
     });

     //用于递归改变json的属性
     function changeProperity(list) {
         $.each(list, function (index, value) {
             value.checked = value.check ? true : false;
             if (value.children) {
                 changeProperity(value.children);
             }
         });
     }

     var setting = {
         view: {
             addHoverDom: addHoverDom,
             removeHoverDom: removeHoverDom,
             selectedMulti: false
         },
         edit: {
             enable: true,
             editNameSelectAll: true
         },
         check: {
             enable: true
         },
         callback: {
             onCheck: onCheck,
             onDblClick: onClick,
             onRightClick: onRightClick,
             beforeDrag: beforeDrag,
             beforeRemove: beforeRemove,
             onRemove: onRemove
         }
     };

     function beforeDrag(treeId, treeNodes) {
         return false;
     }

     function onClick(event, treeId, treeNode, clickFlag) {
         var url = '@Url.Action("Edit", "DataProduct", new {id=10000})';
         url = url.replace("10000", treeNode.id);
         window.location.href = url;
     }

     function onCheck(event, treeId, treeNode) {
         updateHot(treeNode.id, treeNode.checked);
     }


     function beforeRemove(treeId, treeNode) {
         var zTree = $.fn.zTree.getZTreeObj("productTree");
         zTree.selectNode(treeNode);
         return confirm("确认删除 节点 -- " + treeNode.name + " 吗？删除该节点会同时删除其所有子节点");
     };

     function onRightClick(event, treeId, treeNode) {
         var url = '@Url.Action("DetailSeo", "DataProduct", new {id=10000})';
         url = url.replace("10000", treeNode.id);
         window.location.href = url;
         return false;
     }

     function onRemove(e, treeId, treeNode) {
         jQuery.ajax({
             type: "post",
             url: '@Url.Action("Delete", "DataProduct")',
             data: {
                 id: treeNode.id
             },
             success: function (data) {
             },
             error: function (data) {
                 alert("删除失败");
                 window.location.href = '@Url.Action("List", "DataProduct")';
             }
         });
     };


     function addHoverDom(treeId, treeNode) {
         var sObj = $("#" + treeNode.tId + "_span");
         if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
         var addStr = "<span class='button add' id='addBtn_" + treeNode.id
				+ "' title='新增' onfocus='this.blur();'></span>";
         sObj.after(addStr);
         var btn = $("#addBtn_" + treeNode.id);
         if (btn) btn.bind("click", function () {

             var id = parseInt(treeNode.id);
             if (id >= 10000000) {
                 alert('该级无法新增');
                 return;

             }
             jQuery.ajax({
                 type: "post",
                 url: '@Url.Action("Add", "DataProduct")',
                 data: {
                     pid: treeNode.id
                 },
                 success: function (data) {
                     var zTree = $.fn.zTree.getZTreeObj("productTree");
                     zTree.addNodes(treeNode, { id: data.id, pId: treeNode.id, name: data.name });
                 }
             });

         });
     };

     function removeHoverDom(treeId, treeNode) {
         $("#addBtn_" + treeNode.id).unbind().remove();
     };

     function updateHot(itemid, ishot) {
         jQuery.ajax({
             type: "post",
             url: '@Url.Action("Hot", "DataProduct")',
             data: {
                 id: itemid,
                 ishot: ishot
             },
             success: function (data) {
             }
         });
     };

    </script>
}
